﻿using HuabanAutoPost.Helper;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace HuabanAutoPost.HuabanHelper
{
    class Publish
    {

        public static void GetTicket(String FileUrl,String description, string Content,String SourceUrl)
        {
            String html = WebHelper.GetHtml("https://huaban.com/bookmarklet/?media=" + GetHtmlEnCode(FileUrl)  
                + "&w=400&h=600&description="+ GetHtmlEnCode(description) + "&url=" + GetHtmlEnCode(SourceUrl));
            ;

            String ticket = Regex.Match(html, "file_ticket\"] = [0-9]{0,20};").Value;
            
            ticket = Regex.Match(ticket, "[0-9]{1,20}").Value;
            Console.WriteLine("已经读取到文件信息：" + ticket);


            CreateShare(ticket, Content,SourceUrl);
        }

        public static string GetHtmlEnCode(string Text) {

            return System.Web.HttpUtility.UrlEncode(Text, Encoding.UTF8);
        }
        private static void CreateShare(String Ticket,String Content,string SourceUrl)
        {//
       
            NameValueCollection loginData = new NameValueCollection();
            loginData.Add("board_id", "39563230");
            loginData.Add("text", Content);
            loginData.Add("via", "2");
            loginData.Add("media_type", "2");
            loginData.Add("video", "0");
            loginData.Add("file_ticket", Ticket);

            loginData.Add("link", SourceUrl);
            loginData.Add("check", "true");
            loginData.Add("share_button", "0");
            WebHelper.PostTohuaban(loginData, "https://huaban.com/pins/");

        }
    }
}
